const app = getApp();
export const OBSdownloadImg = function (fileName, url, fn) {
    const filePath = uni.env.USER_DATA_PATH + '/' + fileName + '.jpg';
    uni.downloadFile({
        url,
        method: 'GET',
        filePath,
        success: (fileRes) => {
            if (fn) {
                fn();
            }
            uni.saveImageToPhotosAlbum({
                filePath: fileRes.filePath,
                success: () => {
                    app.globalData.message.toast('保存成功');
                },
                fail: (err) => {
                    app.globalData.message.toast('保存失败');
                    console.log(err);
                }
            });
        },
        fail: (err) => {
            app.globalData.message.toast('下载文件失败', err);
            console.log(err);
        }
    });
};
export const OBSdownloadPdf = function (fileName, url, fn) {
    const filePath = uni.env.USER_DATA_PATH + '/' + fileName + '.pdf';
    uni.downloadFile({
        url,
        method: 'GET',
        filePath,
        success: (fileRes) => {
            if (fn) {
                fn();
            }
            uni.getSystemInfo({
                success: (systemInfo) => {
                    if (systemInfo.platform == 'windows') {
                        uni.saveFileToDisk({
                            filePath: fileRes.filePath,
                            success: () => {},
                            fail: (saveFileErr) => {
                                app.globalData.message.toast('保存文件错误', saveFileErr);
                                console.log(saveFileErr);
                            }
                        });
                    } else {
                        uni.openDocument({
                            filePath,
                            showMenu: true,
                            success: () => {},
                            fail: (openErr) => {
                                app.globalData.message.toast('文件预览出错', openErr);
                                console.log(openErr);
                            }
                        });
                    }
                },
                fail: (systemInfoErr) => {
                    app.globalData.message.toast('获取系统信息错误', systemInfoErr);
                }
            });
        },
        fail: (err) => {
            app.globalData.message.toast('下载文件失败', err);
            console.log(err);
        }
    });
};
